<template>
  <el-drawer
    title="添加 / 编辑 TLS证书"
    size="50%"
    :visible.sync="visible">
    <template slot="title">
      <span class="title">添加 / 编辑</span>
      <div class="btn-wrap">
        <el-button size="small" @click="close">关闭</el-button>
        <el-button type="primary" size="small" @click="save">保存</el-button>
      </div>
    </template>
    <div class="modify common-container">
      <div class="common-bot">
        <div class="modify-bot">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item required label="名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item required label="Cert">
              <el-input v-model="form.tlsCert" type="textarea"
                        :rows="16"></el-input>
            </el-form-item>
            <el-form-item required label="Key">
              <el-input v-model="form.tlsKey" type="textarea"
                        :rows="16"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import Api from "@/api/";

export default {
  data() {
    return {
      visible: false,
      form: {},
    };
  },
  methods: {
    show(id) {
      this.visible = true
      if (id) this.getInfo(id)
      else this.resetForm()
    },
    close() {
      this.visible = false
      this.$emit('close')
    },
    resetForm() {
      this.form = {
        formId: 0,
        name: "",
        tlsCert: "",
        tlsKey: ""
      };
    },
    getInfo(id) {
      Api.tls.detail({
        targetId: id,
      }).then((res) => {
        this.form = res.data
        this.form.formId = res.data.id
      });
    },
    save() {
      Api.tls.modify(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.close()
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.modify {
  .modify-bot {
    width: 100%;
    padding: 30px 10px;
  }
}
</style>
